﻿using System.Text;
using System.Web.UI;

namespace Accumulation.Common {
    /// <summary>
    /// 脚本方法
    /// <remarks>
    /// MatchZhou - 2009/1/19
    /// </remarks>
    /// </summary>
    public class Scripts {
        /// <summary>
        /// 弹出信息，只是弹出了信息，未中断当前页面的操作
        /// </summary>
        /// <param name="info">信息文本</param>
        /// <param name="alertType">弹出信息类型</param>
        /// <param name="stepORurl">后退步数或转向 URL 或 js 代码，例：history.go(-1), location.href='url'</param>
        /// <param name="page">注册脚本页对象</param>
        public static void Alert(string info, AlertType alertType, string stepORurl, Page page) {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script language=\"javascript\" type=\"text/javascript\">");
            sb.AppendFormat("alert(\"{0}\");", info);
            switch (alertType) {
                case AlertType.RedirectToURL:
                    sb.AppendFormat("document.location.href=\"{0}\";", stepORurl);
                    break;
                case AlertType.GoBack:
                    sb.AppendFormat("history.go({0});", stepORurl);
                    break;
                case AlertType.ClosePage:
                    sb.Append("opener = null; window.close();");
                    break;
                case AlertType.SpecifiedProcess:
                    sb.Append(stepORurl);
                    break;
                default:
                    break;
            }
            sb.Append("</script>");
            page.ClientScript.RegisterStartupScript(typeof(string), "AlertInformation", sb.ToString());
        }
        /// <summary>
        /// 执行指定 JavaScript 代码
        /// </summary>
        /// <param name="scriptCode">JavaScript（不包括前后的 script 标签）</param>
        /// <param name="page">注册脚本页对象</param>
        public static void Execute(string scriptCode, Page page) {
            Execute(scriptCode, "ClientScripts", page);
        }
        /// <summary>
        /// 执行指定 JavaScript 代码
        /// </summary>
        /// <param name="scriptCode">JavaScript（不包括前后的 script 标签）</param>
        /// <param name="key">要注册的脚本的键</param>
        /// <param name="page">注册脚本页对象</param>
        public static void Execute(string scriptCode, string key, Page page) {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script language=\"javascript\" type=\"text/javascript\">");
            sb.Append(scriptCode);
            sb.Append("</script>");
            page.ClientScript.RegisterStartupScript(typeof(string), key, sb.ToString());
        }
    }
}
